<?php
	require_once('config.php');
	require_once('include/lsamp_curl.php');
	if(isset($_POST['cvv2'])){
		$vm_client = new lsamp_cURL(FALSE,'','gzip','','',30);
		//add merchant_id, user_id, and pin to the post data
		$post_data = "ssl_merchant_id=" . $ssl_merchant_id . "&ssl_user_id=" . $ssl_user_id . "&ssl_pin=" . $ssl_pin;
		//add card number, expired date, and cvv2 code
		$post_data .= "&ssl_card_number=" . $_POST['cardnumber'] . "&ssl_exp_date=" . $_POST['expiry_moth'] . $_POST['expiry_year']
					."&ssl_cvv2cvc2_indicator=1&ssl_cvv2cvc2=" . $_POST['cvv2'];

		//add amount and sale tax
		$post_data .= "&ssl_amount=" . $_POST['amount'] ."&ssl_salestax=$ssl_salestax";
		//add other required fields
		$post_data .= "&ssl_show_form=true&ssl_test_mode=true&ssl_transaction_type=ccsale";
		//ad receipt format
		$post_data .= "&ssl_result_format=ASCII&ssl_test_mode=$ssl_test_mode";
		$result = $vm_client->post($vm_url, $post_data, $errno, $error);
		error_log($result, 3, 'logs.txt');
 		if($errno !== 0){
			die("Error: " . $error);
		}else{
			$times = preg_match('/\r\n\r\n(.*)/sm',$result, $matches);
			if($times == 1){
				$lines = preg_split('/\n/m', $matches[1]);
				if(count($lines) >= 1){
					$return_info = array();
					foreach($lines as $line){
						preg_match('/(.*)=(.*)/', $line, $tmp);
						$return_info[$tmp[1]] = $tmp[2];
					}
					// create message to send to client using '|' as the field separator
					$return_message = '';
					if(isset($return_info['errorCode'])){
						$return_message = $return_info['errorCode'] . "|An error occur while processing payment data: \n" . $return_info['errorMessage'];
					}else{
						if(isset($return_info['ssl_result']) ){
							if($return_info['ssl_result'] == '0'){
								$return_message .= $return_info['ssl_result']. "|" . $return_info['ssl_txn_id'];
								$return_message .= "|" . $return_info['ssl_approval_code'];
							}else{
								$return_message .= $return_info['ssl_result'] . "|" . $return_info['ssl_result_message'];
							}
						}else{
							$return_message .= "-1|No ssl response code found. Dumping post reponse: $result";
						}
					}

				}else{
					$return_message .= "-1|No contents found: Dumping HTTP post reponse: $result";
				}
				echo $return_message;
			}else{
				echo "-1|An unknown format error occured: Dumping post reponse: $result";
			}

		}
	}
?>